<!DOCTYPE html>
 <html>
   <head>
      <style type="text/css">
         .block {
            background-color: #ccc;
            padding: 1em;
            margin: 1em;
         }
      </style>
   </head>
   <body>
      <h1>Key-Value Store Client</h1>
      <noscript>
         <span style="color: #f00; font-weight: bold;">
            You need to turn on JavaScript.
         </span>
      </noscript>
      <div class="block"><button onclick="getAll()">Get all</button> Retrieve all key-value pairs from store and log to console.</div>
      <div class="block"><button onclick="deleteAll()">Delete all</button> Delete all data from store.</div>
      <div class="block">
         <form>
            <p>Key: <input id="form_key" type="text" size="50" maxlength="50" value="key1"></p>
            <p>Value 1: <input id="form_value1" type="text" size="50" maxlength="50" value="foobar"></p>
            <p>Value 2: <input id="form_value2" type="text" size="50" maxlength="50" value="123"></p>
            <p>Value 3: <input id="form_value3" type="checkbox" checked></p>
         </form>
         <p><button onclick="setKeyValue()">Set Value</button> Set key-value pair.</p>
         <p><button onclick="deleteKey()">Delete Key</button> Delete key-value by key.</p>
         <p><button onclick="getValue()">Get Key</button> Get key-value by key.</p>
      </div>
      <pre id="log" style="height: 20em; overflow-y: scroll; background-color: #faa;"></pre>

      <script src="/autobahn.min.js"></script>

      <script type="text/javascript">
         var session = null;

         // the URL of the WAMP Router (e.g. Crossbar.io)
         //
         var wsuri;
         if (document.location.origin == "file://") {
            wsuri = "ws://localhost:9000";
         } else {
            wsuri = "ws://" + document.location.hostname + ":9000";
         }

         // connect to WAMP server
         //
         var connection = new autobahn.Connection({
            url: wsuri,
            realm: 'realm1'
         });

         connection.onopen = function (new_session) {
            console.log("connected to " + wsuri);
            session = new_session;
         };

         connection.onclose = function (reason, details) {
            console.log("connection gone", reason, details);
            new_session = null;
         }

         connection.open();

         function setKeyValue() {

            var key = document.getElementById('form_key').value;
            var value = {
               value1: document.getElementById('form_value1').value,
               value2: document.getElementById('form_value2').value,
               value3: document.getElementById('form_value3').checked,
               modified: new Date()
            }

            session.call("com.example.keyvalue.set", [key, value]).then(
               function () {
                  console.log("Value stored!");
               }
            );
         }

         function getValue() {

            var key = document.getElementById('form_key').value;

            session.call("com.example.keyvalue.get", [key]).then(
               function (value) {
                  console.log(value);
                  document.getElementById('form_value1').value = value.value1;
                  document.getElementById('form_value2').value = value.value2;
                  document.getElementById('form_value3').checked = value.value3;
               }
            );
         }

         function deleteKey() {

            var key = document.getElementById('form_key').value;

            session.call("com.example.keyvalue.set", [key]).then(
               function () {
                  log("Key-Value deleted!");
               }
            );
         }

         function getAll() {
            session.call("com.example.keyvalue.get").then(
               function (res) {
                  console.log(res);
               }
            );
         }

         function deleteAll() {
            session.call("com.example.keyvalue.set").then(
               function () {
                  log("All Key-Values deleted!");
               }
            );
         }
     </script>
   </body>
 </html>
